package com.ydy.controller;

import com.ydy.pojo.Items;
import com.ydy.pojo.ItemsImg;
import com.ydy.pojo.ItemsParam;
import com.ydy.pojo.ItemsSpec;
import com.ydy.pojo.vo.*;
import com.ydy.service.ItemService;
import com.ydy.utils.PageInfo;
import com.ydy.utils.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "商品接口", tags = {"商品信息展示的相关接口"})
@RestController
@RequestMapping("items")
public class ItemController {

    @Autowired
    private ItemService itemService;

    /**
     * 查询商品相关信息
     * @Title: info
     * @author: dy.yin 2022/1/14 15:16
     * @param: [itemId]  商品id
     * @return: {@link ReturnResult 统一返回}
     */
    @ApiOperation(value = "查询商品详情", notes = "查询商品详情", httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public ReturnResult info(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @PathVariable String itemId) {

        if (StringUtils.isBlank(itemId)) {
            return ReturnResult.errorMsg("fuck you");
        }

        //查询商品信息
        Items item = itemService.queryItemById(itemId);
        List<ItemsImg> itemImgList = itemService.queryItemsImgById(itemId);
        List<ItemsSpec> itemSpecList = itemService.queryItemsSpecById(itemId);
        ItemsParam itemsParams = itemService.queryItemsParamById(itemId);

        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(item);
        itemInfoVO.setItemImgList(itemImgList);
        itemInfoVO.setItemSpecList(itemSpecList);
        itemInfoVO.setItemParams(itemsParams);

        return ReturnResult.ok(itemInfoVO);
    }

    /**
     * 根据商品id查询商品的评价等级数量
     * @Title: commentLevel
     * @author: dy.yin 2022/1/14 17:04
     * @param: [itemId] 商品id
     * @return: {@link ReturnResult }统一返回
     */
    @ApiOperation(value = "查询商品评价等级", notes = "查询商品评价等级", httpMethod = "GET")
    @GetMapping("/commentLevel")
    public ReturnResult commentLevel(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @RequestParam String itemId) {

        if (StringUtils.isBlank(itemId)) {
            return ReturnResult.errorMsg("fuck you");
        }

        CommentLevelCountsVO countsVO = itemService.queryCommentCounts(itemId);

        return ReturnResult.ok(countsVO);
    }

    /**
     * 查询商品评论
     * @param itemId 商品id
     * @param level 评论级别
     * @param page 当前第几页
     * @param pageSize 每页多少条
     * @return
     */
    @ApiOperation(value = "查询商品评论", notes = "查询商品评论", httpMethod = "GET")
    @GetMapping("/comments")
    public ReturnResult comments(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @RequestParam String itemId,
            @ApiParam(name = "level", value = "评价等级", required = false)
            @RequestParam Integer level,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {

        if (StringUtils.isBlank(itemId)) {
            return ReturnResult.errorMsg("fuck you");
        }

        PageInfo pageInfo = new PageInfo(page,pageSize);
        List<ItemCommentVO> grid = itemService.queryPagedComments(itemId,level,pageInfo);
        pageInfo.setPageResult(grid);

        return ReturnResult.ok(pageInfo);
    }

    /**
     * 搜索商品列表
     * @param keywords 搜索关键字
     * @param sort 排序方式
     * @param page 当前第几页
     * @param pageSize 每页条数
     * @return
     */
    @ApiOperation(value = "搜索商品列表", notes = "搜索商品列表", httpMethod = "GET")
    @GetMapping("/search")
    public ReturnResult search(
            @ApiParam(name = "keywords", value = "关键字", required = true)
            @RequestParam String keywords,
            @ApiParam(name = "sort", value = "排序", required = false)
            @RequestParam String sort,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {

        PageInfo pageInfo = new PageInfo(page,pageSize);
        List<SearchItemsVO> list = itemService.searchItemsByKeyWords(keywords,sort,pageInfo);
        pageInfo.setPageResult(list);

        return ReturnResult.ok(pageInfo);
    }

    /**
     * 通过分类id搜索商品列表
     * @param catId 分类id
     * @param sort 排序方式
     * @param page 当前页数
     * @param pageSize 每页条数
     * @return
     */
    @ApiOperation(value = "通过分类id搜索商品列表", notes = "通过分类id搜索商品列表", httpMethod = "GET")
    @GetMapping("/catItems")
    public ReturnResult catItems(
            @ApiParam(name = "catId", value = "三级分类id", required = true)
            @RequestParam Integer catId,
            @ApiParam(name = "sort", value = "排序", required = false)
            @RequestParam String sort,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {

        if (catId == null) {
            return ReturnResult.errorMsg("fuck you");
        }
        PageInfo pageInfo = new PageInfo(page,pageSize);
        List<SearchItemsVO> list = itemService.searchItemsByCatId(catId,sort,pageInfo);
        pageInfo.setPageResult(list);

        return ReturnResult.ok(pageInfo);
    }

    /**
     * 用于用户长时间未登录网站，刷新购物车中的数据（主要是商品价格），类似京东淘宝
     * @param itemSpecIds 商品规格id
     * @return
     */
    @ApiOperation(value = "根据商品规格ids查找最新的商品数据", notes = "根据商品规格ids查找最新的商品数据", httpMethod = "GET")
    @GetMapping("/refresh")
    public ReturnResult refresh(
            @ApiParam(name = "itemSpecIds", value = "拼接的规格ids", required = true, example = "1001,1003,1005")
            @RequestParam String itemSpecIds) {

        if (StringUtils.isBlank(itemSpecIds)) {
            return ReturnResult.ok();
        }

        List<ShopCartVO> list = itemService.queryItemsBySpecIds(itemSpecIds);
        return ReturnResult.ok(list);
    }
}
